/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.Comment;
import entities.Restaurant;
import entities.Spot;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

/**
 *
 * 
 * author Ahmet
 */
@ManagedBean
@ViewScoped
public class WizardBean {
    
    @PersistenceContext 
    private EntityManager em;
    @Resource 
    private UserTransaction utx;
    private Spot spot;
//    private String keyword;
//    private Integer restId;
    private Spot selectedSpot;
//    private List<Restaurant> restList;
    
    public WizardBean() {
        spot = new Spot();
    }

//    public List<Restaurant> getRestList() {
//        return restList;
//    }
//
//    public void setRestList(List<Restaurant> restList) {
//        this.restList = restList;
//    }
//    
//    private String restName;
//
//    public String getRestName() {
//        return restName;
//    }
//
//    public void setRestName(String restName) {
//        this.restName = restName;
//    }
//
//    public String getKeyword() {
//        return keyword;
//    }
//
//    public void setKeyword(String keyword) {
//        this.keyword = keyword;
//    }

    public Spot getSpot() {
        return spot;
    }

    public void setSpot(Spot spot) {
        this.spot = spot;
    }

    public Spot getSelectedSpot() {
        return selectedSpot;
    }

    public void setSelectedSpot(Spot selectedSpot) {
        this.selectedSpot = selectedSpot;
    }

//    public Integer getRestId() {
//        return restId;
//    }
//
//    public void setRestId(Integer restId) {
//        this.restId = restId;
//    }
//
////    public void setRestId(Object restId) {
////        this.restId = (Integer)restId;
////        //this.spot.setRid((Restaurant) em.createNamedQuery("Restaurant.findByRid").setParameter("rid", this.restId.intValue()).getSingleResult());
////    }
//
//    public void findRestaurants() {
//         restList = (List<Restaurant>) em.createNamedQuery("Restaurant.findByKeyword").setParameter("keyword", keyword).getResultList();
//             
////             for(Restaurant r: restList){
////                 SelectItem i = new SelectItem(r.getRid(), r.getName()+"-"+r.getLocation());
////                 selectedRests.add(i);
////             }
////             int a=1;
//    }

    public List<Spot> listSpots() {
        try {
            return (List<Spot>) em.createNamedQuery("Spot.findAll").getResultList();
        } catch (NoResultException nre) {
            return null;
        }
    }
//    
//    public List<Comment> getComments() {
//        try {
//            return (List<Comment>) em.createNamedQuery("Spot.findBySpotID").setParameter("sid", this).getResultList();
//        } catch (NoResultException nre) {
//            return null;
//        }
//    }

//    public List<String> complete(String query) {        
//        List<String> results = new ArrayList<String>();        
//        
//        List<Restaurant> rests= (List<Restaurant>) em.createNamedQuery("Restaurant.findByKeyword").setParameter("keyword", keyword).getResultList();
//       
//        for (int i = 0; i < rests.size(); i++) {            
//            results.add(rests.get(i).getName() + " - "+ rests.get(i).getLocation());            
//        }        
//        
//        return results;        
//    }    
    
    public void save() {
        System.out.println(spot.getRid().getName());
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean loginBean = (LoginBean) context.getExternalContext().getSessionMap().get("loginBean");
        NewBean newBean = (NewBean) context.getExternalContext().getSessionMap().get("newBean");
        spot.setRid(newBean.getSelectedRest());
//        spot.setUsername((User)context.getExternalContext().getSessionMap().get("user"));
        spot.setUsername(loginBean.getUser());
//        setKeyword("burger");
//        spot.setRid((Restaurant)findRestaurants().get(0));
        if(spot.getPic()==null || spot.getPic().trim().equals(""))
                spot.setPic("defaultFoodspotting.png");
        try {
            utx.begin();
            em.persist(spot);
            utx.commit();
            
            FacesMessage msg = new FacesMessage("Successful", "New Spot :" + spot.getName() + " has been added!");  
            context.addMessage(null, msg); 
            
        } catch (Exception e) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Error creating Spot!",
                    "Unexpected error when creating this spot.  Please contact the system Administrator " + spot.getUsername().getLname()+ " - " + loginBean.getUser().getFname() + " - " + spot.getRid().getLocation() + " - " + spot.getRid().getName());
            context.addMessage(null, message);
            Logger.getAnonymousLogger().log(Level.SEVERE,
                    "Unable to create new spot",
                    e);

        }
    }
}
